package kr.pe.johnkim.pushchat.mobile.controller;

import kr.pe.johnkim.pushchat.mobile.domain.PushChatChat;
import kr.pe.johnkim.pushchat.mobile.service.PushChatChatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 6. 12.
 */
@Controller
@RequestMapping(value = "/pushChatChatController")
public class PushChatChatController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(PushChatChatController.class);

    @Autowired
    private PushChatChatService pushChatChatService;

    /**
     * 채팅방 중복 확인을 한다. 참여자가 2명일때만 확인한다.
     *
     * @param pushChatChat
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectChatDuplicate", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatChat selectChatDuplicate(@RequestBody PushChatChat pushChatChat) throws Exception {
        logger.info("selectChatDuplicate");
        return this.pushChatChatService.selectChatDuplicate(pushChatChat);
    }

    /**
     * 채팅방을 개설 한다.
     * 채팅방 개설과 사용자 추가를 한개의 transaction 으로 묶는다.
     *
     * 채팅방을 생성하고 개설자를 사용자로 추가한 뒤 채팅방의 idChat, owner 를 넘겨준다.
     * 넘겨받은 파라메터를 사용해서 다른 사용자를 채팅방에 추가한다.
     *
     * @param pushChatChat
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/registerChatAndRegisterUser", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatChat registerChatAndRegisterUser(@RequestBody PushChatChat pushChatChat) throws Exception {
        logger.info("registerChatAndRegisterUser");

        PushChatChat result = new PushChatChat();

        try {
            result = this.pushChatChatService.registerChatAndRegisterUser(pushChatChat);
            result.setResult("SUCCESS");
            logger.info("success to register chat and user.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to register chat and user.");
        }

        return result;
    }

    /**
     * 채팅방 정보를 가져 온다. 채팅방을 개설할때 호출한다.
     *
     * @param pushChatChat
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectChatInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatChat selectChatInformation(@RequestBody PushChatChat pushChatChat) throws Exception {
        logger.info("selectChatInformation");

        return this.pushChatChatService.selectChatInformation(pushChatChat);
    }

    /**
     * 채팅방 목록을 가져 온다.
     *
     * @param pushChatChat
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectChatList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<PushChatChat> selectChatList(@RequestBody PushChatChat pushChatChat) throws Exception {
        logger.info("selectChatList");
        return this.pushChatChatService.selectChatList(pushChatChat);
    }

    /**
     * 채팅방을 비활성화 한다.
     *
     * @param pushChatChat
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/updateDeactivateChat", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatChat updateDeactivateChat(@RequestBody PushChatChat pushChatChat) throws Exception {
        logger.info("updateDeactivateChat");

        PushChatChat result = new PushChatChat();

        try {
            this.pushChatChatService.updateDeactivateChat(pushChatChat);
            result.setResult("SUCCESS");
            logger.info("success to update deactivate chat.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update deactivate chat.");
        }

        return result;
    }
}
